import React, { useState ,useReducer } from 'react';

//count 是一个变量对应useReducer第二个参数赋值
//useReducer 第一个参数对应redux的reducer
//dispatch 是子组件传dispatch，不是回调函数，不会再组件重新渲染时改变
function Reducer(){
    const [count,dispatch] = useReducer((state,action)=>{
        switch (action.type) {
            case 'add':
                return state + 1
                break;
            case 'sub':
                return state - 1
                break;
            default:
                break;
        }
    },0)

    return(
        <>
            <h1>{count}</h1>
            <button onClick={()=>{dispatch({type:'add'})}}>increment</button>
            <button onClick={()=>{dispatch({type:'sub'})}}>decrement</button>
        </>
    )
}

export default Reducer;
